<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">

    <title>合同列表</title>

    <meta name="pragma" content="no-cache">
    <meta name="cache-control" content="no-cache">
    <meta name="expires" content="0">
    <div th:replace="~{commons/script::script}"></div>
</head>
<body>
<div>
    <select class="easyui-combobox theme-textbox-radius" style="width:120px" id="contractOne">
        <option value="-1">请选择筛选条件</option>
        <option value="customerName">客户</option>
        <option value="contactsName">联系人</option>
        <option value="userName">负责人</option>
    </select>
    <select class="easyui-combobox theme-textbox-radius" style="width:100px" id="contractTwo">
        <option value="-1">请选择筛选条件</option>
        <option value="baoHan">包含字段</option>
        <option value="all">精确查询</option>
    </select>
    <span>查询:</span>
    <input id="contract" style="line-height:26px;border:1px solid #ccc">
    <a href="javascript:void(0);" class="easyui-linkbutton" plain="true" onclick="doSearch()" data-options="iconCls:'icon-search',plain:true">搜索</a>
    <span onclick="return myOwner('/contract/my')" class="easyui-linkbutton" data-options="plain:true">我负责的</span>|
    <span onclick="return myCreate('/contract/my')" class="easyui-linkbutton" data-options="plain:true">我创建的</span>
    <span onclick="return todaySigned('/contract/todaySigned')" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true">今日签约</span>|
    <span onclick="return weekSigned('/contract/todaySigned')" class="easyui-linkbutton" data-options="plain:true">本周签约</span>|
    <span onclick="return monthSigned('/contract/todaySigned')" class="easyui-linkbutton" data-options="plain:true">本月签约</span>|
    <!--<span id="reset" class="easyui-linkbutton" data-options="iconCls:'icon-update',plain:true">重置</span>-->
</div>
<table id="contractList" class="easyui-datagrid"></table>
<div id="toolbar">
    <a href="javascript:void(0);" onclick="return add('/contract/toAdd')" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">新建</a>
    <a href="javascript:void(0);" onclick="return del('/contract/delete')" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
    <a href="javascript:void(0);" onclick="return edit('/contract/toEdit')" id="editBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
</div>
</div>
<script type="text/javascript">
    //条件查询
    function doSearch(){
        $('#contractList').datagrid('load',{
            "contractName":$('#contract').val(),
            "contractOne":$("#contractOne").val(),
            "contractTwo":$("#contractTwo").val()
        });
    }
    //添加合同
    function add(url){
        parent.parent.openTopWindow({
            width:800,
            height:600,
            title:"添加合同",
            "url": url,
            close:function(){
                $("#positionList").datagrid("reload");
            }

        });
    }
    //修改部门
    function edit(url){
        var rows = $("#contractList").datagrid("getChecked");
        if(!rows.length){
            $.messager.alert("警告","请选择要修改的数据");
            return false;
        }
        var id=rows[0].id;
        parent.parent.openTopWindow({
            width:800,
            height:600,
            title:"修改岗位信息",
            "url": url+"?id="+id,
            close:function(){
                $("#contractList").datagrid("reload");
            }

        });
    }


    //删除部门
    function del(url){
        //获取到选中的一行数据
        var rows = $("#contractList").datagrid("getChecked");
        if(!rows.length){
            $.messager.alert("警告","请选择要删除的数据");
            return;
        }

        $.messager.confirm("警告","数据删除后无法恢复，是否确认删除",function(b){
            if(b){
                var ids = new Array();
                $.each(rows,function(index,obj){
                    ids.push(obj.id);
                });
                //将数组中元素使用,分割拼接为一个字符串
                ids = ids.join(",");
                $.post(url,
                    {
                        "ids" : ids
                    },
                    function(data){
                        if(data && data.success){
                            $.messager.alert("提示",data.message);
                            $("#contractList").datagrid("reload");
                            return;
                        }
                        $.messager.alert("警告",data.message);
                    },"json");
            }
        });
    }

    //加载用户数据
    $(function(){
        $("#contractList").datagrid({
            url : "/contract/listPage",
            //queryParams:{"positionName":$("#positionName").val()},
            pagination : true,
            toolbar : "#toolbar",
            fitColumns : true,
            idField : "id",
            rownumbers : true,
            singleSelect:true,
            showHeader:true,//定义是否显示头部
            columns : [
                [
                {field:"id",title:"选择合同",checkbox:true},
                {field:"number",title:"合同编号"},
                {field:"customerName",title:"客户"},
                {field:"userName",title:"负责人"},//ownerUserName
                {field:"contactsName",title:"联系人"},
                {field:"dueTime",title:"签约日期"},
                {field:"price",title:"合同金额"},
                {field:"status",title:"状态"},
                {field:"endDate",title:"距离到期时间"},
                {field:"operation",title:"操作",formatter:function(value,row,index){
                        return "<a href='javascript:void(0)' onclick='return see("+index+")'>查看</a>"
                       /* "<a href='/contract/toSee?ids='"+row.index+">查看</a>"*/+
                            "&nbsp;&nbsp<a href='javascript:void(0)' onclick='return modify("+index+")'>编辑</a>";
                    }}


            ]
            ],
            loadFilter:function(data){
                //data是服务器返回的数据,将服务器端返回的数据转换为datagrid需要的格式
                return {"total":data.data.totalRows,"rows":data.data.result};
            }

        });
    });

    function see(index){
        $("#contractList").datagrid("selectRow",index);
        var row=$("#contractList").datagrid("getSelected");
        parent.parent.openTopWindow({
            width:900,
            height:600,
            title:"查看商机",
            "url": "/contract/toSee?id="+row.id,
            close:function(){
                $("#contractList").datagrid("reload");
            }
        });
    }

    function modify(index){
        $("#contractList").datagrid("selectRow",index);
        var row=$("#contractList").datagrid("getSelected");
        parent.parent.openTopWindow({
            width:900,
            height:600,
            title:"修改商机",
            "url": "/contract/toEdit?id="+row.id,
            close:function(){
                $("#contractList").datagrid("reload");
            }
        });
    }

    //查询今天创建的客户
    function todaySigned(url) {
        /*$("#businessList").datagrid({
            "url": url + "?type=day"
        })*/
        location.href=url + "?type=day";

    }

    //查询周创建的客户
    function weekSigned(url) {
        location.href=url + "?type=week";
    }

    //查询月创建的客户
    function monthSigned(url) {
        location.href=url + "?type=month";
    }
    function myOwner(url) {
        location.href=url+"?user=owner";
    }
    function myCreate(url) {
        location.href=url+"?user=create";
    }
</script>
</body>
</html>